#郑文超

import time,os
from common.zwc_login import Login
from testcase.financial_approval import financial_flow
from faker import Factory
from testcase.jiemi import quiry
from utils.utility import Utility
from common.report_generate import Report
from selenium.webdriver.support.select import Select

class Financial:

    def __init__(self,username):
        self.driver= financial_flow(username,"财务管理","财务流水")
        self.st = self.driver
        self.connet_sql = Utility()
        self.write_sql = Report()


    def add_pool(self):
        # 解除二级密码
        self.st.driver.find_element_by_id('btn-decrypt').click()
        # time.sleep(1)
        self.st.driver.find_element_by_name('secondPass').send_keys('Woniu123')
        self.st.driver.find_element_by_xpath('//*[@id="secondPass-modal"]/div/div/div[3]/button').click()
        time.sleep(1)
        try:
            self.st.driver.find_element_by_xpath('/html/body/div[9]/div/div/div[3]/button').click()
            print('解密失败')
        except:
            print('解密成功')


    #验证一级科目与二级科目是否关联
    def test_case1(self,s_num,title,caseid):
        self.driver.random_select(self.driver.first_subject(),s_num[0])
        time.sleep(1)
        self.driver.random_select(self.driver.second_subject(),s_num[1])
        img = self.st.screenshot()
        first =self.st.driver.find_element_by_xpath("/html/body/div[7]/div[2]/div/div/div[3]/div[2]/div[2]/table/tbody/tr[1]/td[3]").text
        second =self.st.driver.find_element_by_xpath("/html/body/div[7]/div[2]/div/div/div[3]/div[2]/div[2]/table/tbody/tr[1]/td[4]").text
        if first==s_num[0] and second ==s_num[1]:
            print("测试通过")
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试',title, caseid, '成功', '无', "无")
        else:
            print("测试失败")
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        self.st.driver.close()

    #验证查询结果与查询条件是否一致，例如查询区域：成都
    def test_case2(self,s_num,title,caseid):
        flag = True
        self.st.random_select(self.st.region(),s_num[0])
        img = self.st.screenshot()
        for i in range(1,3):
            xpath = "/html/body/div[7]/div[2]/div/div/div[3]/div[2]/div[2]/table/tbody/tr[%d]/td[1]" % i
            result = self.st.driver.find_element_by_xpath(xpath).text
            # print(f'查询结果为{result}')
            if s_num[0] != result:
                flag = True   #验证通过
            else:
                flag = False  #验证失败
        if flag:
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试',title, caseid, '成功', '无', "无")
        else:
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        self.st.driver.close()



    #只输入开始时间能否查询结果
    def test_case3(self,s_num,title,caseid):
        self.st.s_time().clear()
        self.st.s_time().send_keys(s_num[0])
        self.driver.check().click()
        time=self.st.driver.find_element_by_xpath("/html/body/div[7]/div[2]/div/div/div[3]/div[2]/div[2]/table/tbody/tr[1]/td[10]").text
        print(time)
        # print(re)
        if time==s_num[0]:
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试',title, caseid, '成功', '无', "无")
        else:
            img = self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        self.st.driver.close()

    # 开始时间等于结束时间是否查询出当天记录
    def test_case4(self, s_num, title, caseid):
        self.st.s_time().clear()
        self.st.s_time().send_keys(s_num[0])
        self.st.e_time().send_keys(s_num[1])
        self.driver.check().click()
        try:
            self.st.driver.find_element_by_xpath("/html/body/div[7]/div[2]/div/div/div[3]/div[2]/div[2]/table/tbody/tr[1]/td[12]/button")
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")
        except:
            img = self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        self.st.driver.close()

    # 是否支持手动输入时间查询
    def test_case5(self, s_num, title, caseid):
        self.st.s_time().clear()
        self.st.s_time().send_keys(s_num[0])
        self.st.e_time().send_keys(s_num[1])
        self.driver.check().click()
        re = self.st.driver.find_element_by_xpath("/html/body/div[7]/div[2]/div/div/div[3]/div[2]/div[2]/table/tbody/tr[1]/td[10]").text

        if re == s_num[0]:
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")
        else:
            img = self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        self.st.driver.close()

    #【财务流水】添加收入时，不填写摘要 能否保存成功
    def test_case6(self, s_num, title, caseid):
        self.st.add_income().click()
        wd1=self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[1]/div[1]/select")
        self.st.random_select(wd1, s_num[0])
        wd2 = self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[1]/div[2]/select")
        self.st.random_select(wd2, s_num[1])
        wd3 = self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[2]/div[1]/select")
        self.st.random_select(wd3, s_num[2])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[3]/div[1]/input").send_keys(s_num[3])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[3]/div[2]/input").send_keys(s_num[4])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[4]/div[1]/input").send_keys(s_num[5])
        wd4 =self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[4]/div[2]/select")
        self.st.random_select(wd4,s_num[6])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[6]/div/textarea").send_keys(s_num[7])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/div[2]/button").click()
        result = self.st.driver.find_element_by_xpath("/html/body/div[17]/div/div/div[2]/div").get_attribute('textContent')
        # print(result)
        if result == "新增成功":
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")
            print("保存成功")
        else:
            print('保存失败')
            img= self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        time.sleep(1)
        self.st.driver.find_element_by_xpath("/html/body/div[17]/div/div/div[3]/button").click()
        self.st.driver.close()

    #"添加收入，收入方：工行,保存成功后，工行账户数据是否增加1000
    def test_case7(self, s_num, title, caseid):
        sql = "SELECT SUM(amount) FROM account_detail WHERE account_id='2'"
        result = self.connet_sql.query_one(sql)[0]
        self.st.add_income().click()
        wd1=self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[1]/div[1]/select")
        self.st.random_select(wd1, s_num[0])
        wd2 = self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[1]/div[2]/select")
        self.st.random_select(wd2, s_num[1])
        wd3 = self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[2]/div[1]/select")
        self.st.random_select(wd3, s_num[2])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[3]/div[1]/input").send_keys(s_num[3])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[3]/div[2]/input").send_keys(s_num[4])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[4]/div[1]/input").send_keys(s_num[5])
        wd4 =self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[4]/div[2]/select")
        self.st.random_select(wd4,s_num[6])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[6]/div/textarea").send_keys(s_num[7])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/div[2]/button").click()
        result = self.st.driver.find_element_by_xpath("/html/body/div[17]/div/div/div[2]/div").get_attribute('textContent')
        if result == "新增成功":
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")
            print("保存成功")
        else:
            print('保存失败')
            img= self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        time.sleep(1)
        self.st.driver.find_element_by_xpath("/html/body/div[17]/div/div/div[3]/button").click()
        self.st.driver.close()

    #【财务流水】-【添加支出】时 保存成功后，能否从数据库查询到新增数据
    def test_case8(self, s_num, title, caseid):
        self.st.add_income().click()
        wd1=self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[1]/div[1]/select")
        self.st.random_select(wd1, s_num[0])
        wd2 = self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[1]/div[2]/select")
        self.st.random_select(wd2, s_num[1])
        wd3 = self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[2]/div[1]/select")
        self.st.random_select(wd3, s_num[2])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[3]/div[1]/input").send_keys(s_num[3])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[3]/div[2]/input").send_keys(s_num[4])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[4]/div[1]/input").send_keys(s_num[5])
        wd4 =self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[4]/div[2]/select")
        self.st.random_select(wd4,s_num[6])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[6]/div/textarea").send_keys(s_num[7])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/div[2]/button").click()
        sql = "SELECT remark FROM finance_detail WHERE expend_no='%s'" %s_num[4]
        result =self.connet_sql.query_one(sql)[0]
        print(result,s_num[7])
        # print(result)
        if result == s_num[7]:
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")
            print("保存成功")
        else:
            print('保存失败')
            img= self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        time.sleep(1)
        self.st.driver.find_element_by_xpath("/html/body/div[17]/div/div/div[3]/button").click()

    #页面显示的总进账与数据库数据是否一致
    def test_case9(self, s_num, title, caseid):
        text= self.st.driver.find_element_by_xpath("//*[@id='count-box']").text
        print(text)
        sql = "SELECT SUM(amount) FROM account_detail WHERE account_id='2' OR account_id='1'"
        re = self.connet_sql.query_one(sql)[0]
        if text == re:
            print("测试通过")
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")
        else:
            print("测试失败")
            img= self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)

    #交易金额输入字母，点击保存，是否有提示
    def test_case10(self, s_num, title, caseid):
        self.st.add_income().click()
        wd1=self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[1]/div[1]/select")
        self.st.random_select(wd1, s_num[0])
        wd2 = self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[1]/div[2]/select")
        self.st.random_select(wd2, s_num[1])
        wd3 = self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[2]/div[1]/select")
        self.st.random_select(wd3, s_num[2])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[3]/div[1]/input").send_keys(s_num[3])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[3]/div[2]/input").send_keys(s_num[4])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/form/div/div[4]/div[1]/input").send_keys(s_num[5])
        self.st.driver.find_element_by_xpath("/html/body/div[16]/div/div/div[2]/button").click()
        time.sleep(2)
        result = self.st.driver.find_element_by_xpath("/html/body/div[20]/div/div/div[2]/div").get_attribute('textContent')
        print(result)
        if result == "交易金额格式不正确":
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")
            print("保存成功")
        else:
            print('保存失败')
            img= self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        time.sleep(1)
        self.st.driver.find_element_by_xpath("/html/body/div[20]/div/div/div[3]/button").click()
        self.st.driver.close()

    #"点击修改按钮，再点击保存，是否提示：修改成功,等待管理员审核
    def test_case11(self, s_num, title, caseid):
        self.st.driver.find_element_by_xpath("//*[@id='detail-table']/tbody/tr[1]/td[12]/button").click()
        self.st.driver.find_element_by_xpath("//*[@id='editIncome']/div/div/div[2]/button").click()
        button = self.st.driver.find_element_by_xpath("/html/body/div[17]/div/div/div[2]/div").text
        if button == "修改成功,等待管理员审核":
            print("验证成功")
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '成功', '无', "无")
        else:
            print("验证失败")
            img= self.st.screenshot()
            self.write_sql.write_report('3.5', '财务流水', 'GUI自动化测试', title, caseid, '失败', '无', img)
        self.st.driver.find_element_by_xpath("/html/body/div[17]/div/div/div[3]/button").click()

    #
if __name__ == '__main__':
    fake = Factory.create("zh_CN")
    name = fake.name()
    phone = fake.phone_number
    print(name)
    print(phone)
    start = Financial("超级管理员")
    start.add_pool()








